<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8" />
  <title>文档上传</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 20px;
      background-color: #f5f5f5;
    }
    .container {
      max-width: 500px;
      margin: 0 auto;
      background: white;
      padding: 30px;
      border-radius: 8px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    h1 {
      text-align: center;
      color: #333;
      margin-bottom: 30px;
    }
    .form-group {
      margin-bottom: 20px;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
      color: #555;
    }
    input[type="text"], input[type="file"] {
      width: 100%;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 14px;
    }
    button {
      width: 100%;
      padding: 12px;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 4px;
      font-size: 16px;
      cursor: pointer;
    }
    button:hover {
      background-color: #0056b3;
    }
    .result {
      margin-top: 20px;
      padding: 10px;
      border-radius: 4px;
      text-align: center;
    }
    .success {
      background-color: #d4edda;
      color: #155724;
      border: 1px solid #c3e6cb;
    }
    .error {
      background-color: #f8d7da;
      color: #721c24;
      border: 1px solid #f5c6cb;
    }
    .info {
      background-color: #d1ecf1;
      color: #0c5460;
      border: 1px solid #bee5eb;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>📄 文档上传</h1>
    <form id="uploadForm">
      <div class="form-group">
        <label for="title">文档标题（可选）</label>
        <input type="text" id="title" name="title" placeholder="请输入文档标题" />
      </div>
      <div class="form-group">
        <label for="file">选择文档文件</label>
        <input type="file" id="file" name="file" accept=".txt,.pdf,.docx" required />
        <small style="color: #666;">支持格式：TXT、PDF、DOCX</small>
      </div>
      <button type="submit">🚀 上传文档</button>
    </form>
    <div id="result"></div>
  </div>

  <script>
    document.getElementById('uploadForm').onsubmit = async function(e) {
      e.preventDefault();
      
      const form = e.target;
      const formData = new FormData(form);
      const resultDiv = document.getElementById('result');
      
      // 显示上传中状态
      resultDiv.className = 'result info';
      resultDiv.textContent = '⏳ 正在上传文档...';
      
      try {
        const response = await fetch('/article/upload', {
          method: 'POST',
          body: formData
        });
        
        const data = await response.json();
        
        // 检查全局响应拦截器的格式
        if (response.ok && data.code === 200 && data.data && data.data.success) {
          resultDiv.className = 'result success';
          resultDiv.innerHTML = `
            ✅ 上传成功！
          `;
        } else {
          resultDiv.className = 'result error';
          resultDiv.textContent = `❌ 上传失败：${data.message || '未知错误'}`;
        }
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.textContent = `❌ 上传失败：${error.message}`;
      }
    };
  </script>
</body>
</html> 